package com.ruoyi.datastatistics.service.impl;

import com.ruoyi.common.core.domain.Parameter;
import com.ruoyi.common.core.feign.ModulesSystemFeign;
import com.ruoyi.datastatistics.domain.AgegroupResult;

import com.ruoyi.datastatistics.mapper.BodycountMapper;
import com.ruoyi.datastatistics.service.AgegroupResultSorter;
import com.ruoyi.datastatistics.service.BodycountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class BodycountServiceImpl implements BodycountService {
    @Autowired
    private BodycountMapper bodycountMapper;
    @Autowired
    public ModulesSystemFeign systemFeign;
    @Autowired
    private HomepageServiceImpl homepageService;
    @Override
    public Map<String,Object> getAgegroupstatistics(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<AgegroupResult> Result = bodycountMapper.getAgegroupstatistics(parameter);
            for(AgegroupResult temp : Result){
                temp.setTotalvalue(temp.getManvalue()+temp.getWomanvalue());
            }
            // 定义排序顺序
            List<String> customOrder  = Arrays.asList("幼儿", "成年","老年");
            // 按照自定义顺序排序
            List<AgegroupResult> sortedResults = AgegroupResultSorter.sortByCustomOrder(
                    Result,
                    AgegroupResult::getNAME,
                    customOrder
            );
            dataMap.put("data",sortedResults);
            dataMap.put("message",true);
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String,Object> getAgebracketstatistics(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<AgegroupResult> Result = bodycountMapper.getAgebracketstatistics(parameter);
            for(AgegroupResult temp : Result){
                temp.setTotalvalue(temp.getManvalue()+temp.getWomanvalue());
            }
            // 定义排序顺序
            List<String> customOrder  = Arrays.asList("3岁", "3.5岁","4岁", "4.5岁","5岁", "5.5岁","6岁","6.5岁", "20岁~24岁","25岁~29岁", "30岁~34岁","35岁~39岁", "40岁~44岁","45岁~49岁", "50岁~54岁","55岁~59岁", "60岁~64岁","65岁~69岁", "70岁~74岁","75岁~79岁");
            // 按照自定义顺序排序
            List<AgegroupResult> sortedResults = AgegroupResultSorter.sortByCustomOrder(
                    Result,
                    AgegroupResult::getNAME,
                    customOrder
            );
            dataMap.put("data",sortedResults);
            dataMap.put("message",true);
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;

    }

    @Override
    public Map<String,Object> getUrbanandruralstatistics(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<AgegroupResult> Result = bodycountMapper.getUrbanandruralstatistics(parameter);
            for(AgegroupResult temp : Result){
                temp.setTotalvalue(temp.getManvalue()+temp.getWomanvalue());
            }
            // 定义排序顺序
            List<String> customOrder  = Arrays.asList("城镇幼儿", "农村幼儿", "城镇非体力劳动者", "城镇体力劳动者", "农民", "城镇老年人", "农村老年人");
            // 按照自定义顺序排序
            List<AgegroupResult> sortedResults = AgegroupResultSorter.sortByCustomOrder(
                    Result,
                    AgegroupResult::getNAME,
                    customOrder
            );
            dataMap.put("data",sortedResults);
            dataMap.put("message",true);
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String,Object> getMonthstatistics(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<AgegroupResult> Result = bodycountMapper.getMonthstatistics(parameter);
            for(AgegroupResult temp : Result){
                temp.setTotalvalue(temp.getManvalue()+temp.getWomanvalue());
            }
            List<AgegroupResult> sortedList = Result.stream()
                    .sorted((a1, a2) -> {
                        try {
                            int num1 = Integer.parseInt(a1.getNAME());
                            int num2 = Integer.parseInt(a2.getNAME());
                            return Integer.compare(num1, num2);
                        } catch (NumberFormatException e) {
                            // 处理非数字情况，例如按字符串排序或赋予默认值
                            return a1.getNAME().compareTo(a2.getNAME());
                        }
                    })
                    .collect(Collectors.toList());
            dataMap.put("data",sortedList);
            dataMap.put("message",true);
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String,Object> getNextdeptstatistics(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<AgegroupResult> Result = bodycountMapper.getNextdeptstatistics(parameter);
            if (Result.size() > 0){
                for(AgegroupResult temp : Result){
                    temp.setTotalvalue(temp.getManvalue()+temp.getWomanvalue());
                }
                dataMap.put("data",Result);
                dataMap.put("message",true);
            }else {
                dataMap.put("data",null);
                dataMap.put("message","请在机构管理中添加该机构信息");
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

}
